<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/include.jsp"%>
<head>
	<script src='<s:url value="/dwr/interface/UserService.js" includeParams="none"/>' type="text/javascript"></script>
	<script src='<s:url value="/dwr/interface/RoleService.js" includeParams="none"/>' type="text/javascript"></script>
	<script type="text/javascript" src="<s:url value='/role/RuntimeParam.js' includeParams='none'/>"></script>
	<script type="text/javascript" src="<s:url value='/role/ExtUser.js' includeParams='none'/>"></script>
	<script type="text/javascript" src="<s:url value='/role/ExtRole.js' includeParams='none'/>"></script>
</head>
<body>
<script>
	Ext.onReady(function (){
		var Tree = Ext.tree;
		var loader =  new Tree.TreeLoader({
          			 dataUrl:'<s:url namespace="/role" action="grantDataRight!getChildren.action" includeParams="false"/>',
          			 baseParams:{'role.id':'<s:property value="role.id"/>'}
        });
		loader.on("beforeload",function(nodeloader,node){
			nodeloader.baseParams.myNodeType=node.attributes.myNodeType;
			//nodeloader.baseParams.node=node.attributes.id;
			return true;
		});
		//插件
		var bt=[Ext.User,Ext.Role,Ext.RuntimeParam];
		var selModel = new Tree.DefaultSelectionModel();
		var totleGeneratedFunParam = 0;
		var funParamTemplate = new Ext.XTemplate(
			    '<div id="{funId}" class="sub_right"><table align="center"  cellpadding="1" cellspacing="1" class="tablelist"><tr class="toptitle">',
			    '<td>参数名称</td><td>所属分组</td><td>参数描述</td><td>参数类型</td><td width="50%">参数值</td>',
			    '<tpl for="funParams">',
			    '<tr class="listr1">',
			    '<input name="role.functionParamValues[{[ parent.beginIndex + xindex - 1 ]}].paramId" type="hidden" value="{id}" />',
				'<td>{parameterName}</td><td>{parameterGroup}</td><td>{remark}</td>',
				'<td><tpl if="parameterType==0">SQL语句</tpl><tpl if="parameterType==1">字符串数组</tpl><tpl if="parameterType==2">字符串</tpl><tpl if="parameterType==3">日期</tpl><tpl if="parameterType==4">时间</tpl></td>',
				'<td><ul><li style="float:left;"><input paramType="{parameterType}" type="text" <tpl if="parameterType==3">class="date"</tpl> <tpl if="parameterType==4">class="time"</tpl> name="role.functionParamValues[{[ parent.beginIndex + xindex - 1]}].paramValue" value="{paramValue}"/></li><li style="float:left;"><div class="oparea"></div></li></ul></td>',		
			    '</tr>',
			    '</tpl>',
			    '</tr></table></div>');
					
		selModel.addListener('beforeselect',function(sel,node,oldNode){
			//选择功能时，渲染该功能的参数
			if(oldNode && Ext.fly('fun_'+oldNode.id)){
				Ext.fly('fun_'+oldNode.id).setVisibilityMode(Ext.Element.DISPLAY).hide();
			}
				 Ext.Ajax.request({
	                 url:'<s:url namespace="/role" action="grantDataRight!getFunctionParams.action" includeParams="false"/>',
	                 params:{'role.id':'<s:property value="role.id"/>','node':node.id},
	                 success:function(response){
	                	var res = response.responseText;
						if (res == null || res==''){alert('没有参数');}
						else{
							var funParams = Ext.decode(res);
							
							if(node.attributes.isGenerated){
								Ext.fly('fun_'+node.id).setVisibilityMode(Ext.Element.DISPLAY).show();
							} else{
								var genEles = funParamTemplate.append('funParams',{beginIndex:totleGeneratedFunParam,funParams:funParams,"funId":'fun_'+node.id},true);
								genEles.select('input[type=text]',true).each(function(e){
									if (e.is('.date')){
										new Ext.form.DateField({applyTo:e,format:'Y/m/d'});
									}else if (e.is('.time')){
										new Ext.form.TimeField({applyTo:e,format:'H:i:s'});
									}else{
										var te = new Ext.form.TextField({applyTo:e});
										var ul = e.findParent('ul',0,true)
										for ( i=0;i< bt.length;i++){
											try{
											var t = new bt[i]({e:te});
											var li = ul.createChild({tag:'li',style:'float:left'});
											t.render(li);
											}catch(e){
												
											}

										}
										
										
									}
									
								});
								
								//其他类型
								
								node.attributes.isGenerated=true;
								totleGeneratedFunParam = totleGeneratedFunParam+funParams.length;
								
								
							}
							

						}
		             }
					
				 });
			});
		
   		var tree = new Tree.TreePanel({
        el:'tree-div',
        width: 350,
        height: 300,
        autoScroll:true,
        singleExpand:false,
        animate:true,
        enableDD:true,
        containerScroll: true,
        rootVisible:false,
        selModel:selModel,
        columns:[{
            header:'功能名',
            width:'100%',
            dataIndex:'text',
            renderer:function(v,n,a){
            	return v;
            }
        }], 
        loader:loader
        });
    // set the root node
    var root = new Tree.AsyncTreeNode({
        text: '数据权限',
        draggable:false,
        id:'-1'
    });
    
    tree.setRootNode(root);
    tree.render();
    
    	  var tpl = new Ext.XTemplate('<tpl for=".">',
    	    	  '<input type="hidden" name="role.rowDataRights[{# - 1}].rightId" value="{id}" />',
    	    	  '</tpl>');
  		  Ext.get("tjButton").on("click",function(){
  			var cn = tree.getChecked();
  		   tpl.overwrite('ids',cn);
    	 });
    });
    
</script>

<div class="main">
<div class="main_title"><b>你当前的位置</b>：权限管理-分配数据权限</div>

<s:form  namespace="/role" action="grantDataRight">

<s:hidden name="role.id" id="role_id"/>

<div id="ids"></div>
<table align="center"  cellpadding="1" cellspacing="1" class="tablelist">
	<s:if test="hasErrors()">
		<s:if test="hasFieldErrors()">
			<tr align="center">
				<td class="tableHeader2"><font color="red" size="4"><s:fielderror/></font></td>
				<td class="tableHeader2"></td>
			</tr>
		</s:if>
		<s:if test="hasActionErrors">
			<tr align="center">
				<td class="tableHeader2"><font color="blue"><s:actionerror/></font></td>
				<td class="tableHeader2"></td>
			</tr>
		</s:if>	
	</s:if>
	<tr class="listr2">
		<td width="15%" align="right" class="listr6">角色名</td>
		<td align="left" class="listr5"><s:property value="role.name"/></td>
	</tr>
	<tr class="listr2">
		<td width="15%" align="right" class="listr6">角色描述</td>
		<td align="left" class="listr5"><s:property value="role.detail"/></td>
	</tr>
	<tr class="listr2">
		<td width="15%" align="right" class="listr6">数据权限</td>
		<td align="left" class="listr5">
			<div>
				<div id="tree-div" style="float: left"></div>
				<div id="funParams" style="float: left;width:65%"></div>
				<div style="clear: both;"></div>
			</div>	
		</td>
	</tr>
	<tr class="listr4">    		
		<td colspan="2">
			<s:submit cssClass="btn" onmouseover="this.className='btn_hover'" onmouseout="this.className='btn'" value="提交" id="tjButton"></s:submit>
			<input name="back" type="button" class="btn" onmouseover="this.className='btn_hover'" onmouseout="this.className='btn'" value="返回" onclick="location.href='list.action'" />
		</td>
	</tr>
</table>
</s:form>
</div>
</body>